// LightQueue.java
//
// Copyright (C) 2006-2011 University of Seville 
// (Contact Rafael Corchuelo at corchu at us.es)
//
// The only purpose of this file is to be useful to my students.
// Using it for commercial purposes will be prosecuted.

package queues.implementations;

import java.util.HashMap;
import java.util.Map;

import queues.exceptions.EmptyQueueException;
import queues.interfaces.IQueue;

/**
 * A light implementation of an <code>IQueue</code> structure.
 * 
 * @author Rafael Corchuelo
 */
public class LightQueue<T> implements IQueue<T> {

	/**
	 * Light queues are implemented using a map
	 * 
	 */
	Map<Integer, T> q;

	/**
	 * Constructs a new horizontal queue
	 * 
	 */
	public LightQueue() {
		super();

		q = new HashMap<Integer, T>();
	}

	/**
	 * @see queues.interfaces.IQueue#first()
	 */
	public T first() {
		if (q.size() == 0)
			throw new EmptyQueueException();

		return q.get(0);
		//return null; Para las pruebas con JUnit
	}

	/**
	 * @see queues.interfaces.IQueue#append(Object)
	 */
	public void append(T obj) {
		q.put(q.size(), obj);		
	}

	/**
	 * @see queues.interfaces.IQueue#delete()
	 */
	public void delete() {
		T obj;

		if (q.size() == 0)
			throw new EmptyQueueException();

		q.remove(0);
		for (int i = 1; i <= q.size(); i++) {
			obj = q.get(i);
			q.remove(i);
			q.put(i - 1, obj);
		}
	}

	/**
	 * @see queues.interfaces.IQueue#size()
	 */
	public int size() {
		return q.size();
	}

}
